home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / iritsm3s.zip / CBSPEVAL.C < prev    next >
C/C++ Source or Header  |  1991-12-21  |  4KB  |  100 lines

  1. /******************************************************************************
  2. * CBspEval.c - Bezier curves handling routines - evaluation routines.          *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Mar. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #ifdef __MSDOS__
  8. #include <stdlib.h>
  9. #endif /* __MSDOS__ */
  10.  
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "cagd_loc.h"
  15.  
  16. /******************************************************************************
  17. * Assumes Vec holds control points for scalar bspline curve of order Order    *
  18. * length Len and knot vector KnotVector.                      *
  19. * Evaluates and returns that curve value at parameter value t.              *
  20. * Vec is incremented by VecInc (usually by 1) after each iteration.          *
  21. ******************************************************************************/
  22. CagdRType BspCrvEvalVecAtParam(CagdRType *Vec, int VecInc,
  23.                    CagdRType *KnotVector, int Order, int Len,
  24.                    CagdRType t)
  25. {
  26.     int i, IndexFirst;
  27.     CagdRType
  28.     R = 0.0,
  29.     *BasisFunc = BspCrvCoxDeBoorBasis(KnotVector, Order, Len, t,
  30.                                 &IndexFirst);
  31.  
  32.     if (VecInc == 1) {
  33.     Vec += IndexFirst;
  34.     for (i = 0; i < Order; i++)
  35.         R += BasisFunc[i] * *Vec++;
  36.     }
  37.     else {
  38.     Vec += IndexFirst * VecInc;
  39.     for (i = 0; i < Order; i++) {
  40.         R += BasisFunc[i]  * *Vec;
  41.         Vec += VecInc;
  42.     }
  43.     }
  44.  
  45.     return R;
  46. }
  47.  
  48. /******************************************************************************
  49. * Returns a pointer to a static data, holding the value of the curve at given *
  50. * parametric location t. The curve is assumed to be Bspline.              *
  51. * Uses the Cox de Boor recursive algorithm (is this fastest for single eval?) *
  52. ******************************************************************************/
  53. CagdRType *BspCrvEvalAtParam(CagdCrvStruct *Crv, CagdRType t)
  54. {
  55.     return BspCrvEvalCoxDeBoor(Crv, t);
  56. }
  57.  
  58. /******************************************************************************
  59. * Samples the curves at FineNess location equally spaced in the Bspline       *
  60. * parametric domain.                                  *
  61. * Currently uses the Cox de Boor evaluation.                      *
  62. * Returns the actual number of points in polyline (<= FineNess).          *
  63. ******************************************************************************/
  64. int BspCrvEvalToPolyline(CagdCrvStruct *Crv, int FineNess, CagdRType *Points[])
  65. {
  66.     CagdBType
  67.     IsNotRational = !CAGD_IS_RATIONAL_CRV(Crv);
  68.     int i, Count, NumC1Disconts,
  69.         n = 1 << FineNess,
  70.     Len = Crv -> Length,
  71.     MaxCoord = CAGD_NUM_OF_PT_COORD(Crv -> PType);
  72.     CagdRType *Pt, *C1Disconts, *IsoParams, t, TMin, TMax;
  73.  
  74.     if (Crv -> Order == 2) {         /* Simply copy the control polygon. */
  75.     CagdRType **CrvPoints = Crv -> Points;
  76.  
  77.     for (Count = 0; Count < n && Count < Len; Count++)
  78.         for (i = IsNotRational; i <= MaxCoord; i++)
  79.             Points[i][Count] = CrvPoints[i][Count];
  80.     return Count;
  81.     }
  82.  
  83.     BspCrvDomain(Crv, &TMin, &TMax);
  84.  
  85.     /* Compute discontinuities along the u axis and use that to determine    */
  86.     /* where to extract isolines along u.                     */
  87.     C1Disconts = BspKnotAllC1Discont(Crv -> KnotVector, Crv -> Order,
  88.                      Crv -> Length, &NumC1Disconts);
  89.     IsoParams = BspKnotParamValues(TMin, TMax, n, C1Disconts, NumC1Disconts);
  90.     for (Count = 0; Count < n; Count++) {
  91.      t = IsoParams[Count];
  92.     Pt = BspCrvEvalAtParam(Crv, t);
  93.     for (i = IsNotRational; i <= MaxCoord; i++)
  94.         Points[i][Count] = Pt[i];
  95.     }
  96.     CagdFree((VoidPtr) IsoParams);
  97.  
  98.     return n;
  99. }
  100.